using System;
using System.Collections.Generic;

namespace Custom.Daily.Toolkit
{
    public static class CalendarSelectTools
    {
        private static List<int> monthDays = new List<int> { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

        /// <summary>
        /// 通过 年 和 月 计算第一天是星期几
        /// </summary>
        /// <param name="year">年</param>
        /// <param name="month">月</param>
        /// <returns>周一 到 周日 对应 1 到 7</returns>
        public static int GetFirstDayOfWeek(int year, int month)
        {
            var firstDayOfMonth = new DateTime(year, month, 1);
            switch (firstDayOfMonth.DayOfWeek)
            {
                case DayOfWeek.Sunday:
                    return 7;
                default:
                    return (int)firstDayOfMonth.DayOfWeek;
            }
        }

        /// <summary>
        /// 通过年和月计算对应的天数
        /// </summary>
        /// <param name="year">年</param>
        /// <param name="month">月</param>
        /// <returns>这个月的天数</returns>
        public static int GetDayByYearMonth(int year, int month)
        {
            if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
            {
                if (month == 2)
                {
                    return 29;
                }
            }
            return monthDays[month - 1];
        }

        /// <summary>
        /// 计算上一个月的年和月
        /// </summary>
        /// <param name="year">年</param>
        /// <param name="month">月</param>
        /// <returns>年份</returns>
        public static int[] GetPrevYearMonth(int year, int month)
        {
            int[] arr = new int[2];
            arr[0] = year;
            arr[1] = month - 1 > 0 ? month - 1 : 12;
            if (arr[1] == 12)
            {
                arr[0] = year - 1;
            }
            return arr;
        }
        /// <summary>
        /// 计算下一个月的年和月
        /// </summary>
        /// <param name="year">年</param>
        /// <param name="month">月</param>
        /// <param name="nextMonth">下一个月</param>
        /// <returns>年份</returns>
        public static int[] GetNextYearMonth(int year, int month)
        {
            int[] arr = new int[2];
            arr[0] = year;
            arr[1] = month + 1 > 12 ? 1 : month + 1;
            if (arr[1] == 1)
            {
                arr[0] = year + 1;
            }
            return arr;
        }
    }
}
